package com.tianhui.consignortwofei.mvp.ui.activity;

import android.text.TextUtils;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.mvp.model.UserInfoModel;
import com.tianhui.consignortwofei.mvp.model.enty.statistics.DriverCarStatisticalInfo;
import com.tianhui.consignortwofei.mvp.model.enty.statistics.OrderStatisticalInfo;
import com.tianhui.consignortwofei.mvp.model.enty.statistics.StatisticsItemInfo;
import com.tianhui.consignortwofei.widget.StatisticsInfoView;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * @author fgs
 * @date 2021/2/22
 * @description
 **/
public class StatisticsDataActivity extends BaseActivity {

    @BindView(R.id.activity_statistics_data_todayTJOrderStatisticsInfoView)
    StatisticsInfoView todayTJOrderStatisticsInfoView;
    @BindView(R.id.activity_statistics_data_todayHBOrderStatisticsInfoView)
    StatisticsInfoView todayHBOrderStatisticsInfoView;
    @BindView(R.id.activity_statistics_data_allOrderStatisticsInfoView)
    StatisticsInfoView allOrderStatisticsInfoView;
    @BindView(R.id.activity_statistics_data_driverAndCarStatisticsInfoView)
    StatisticsInfoView driverAndCarStatisticsInfoView;
    @BindView(R.id.activity_statistics_data_todayDriverAndCarStatisticsInfoView)
    StatisticsInfoView todayDriverAndCarStatisticsInfoView;
    private UserInfoModel mModel;

    @Override
    public int getLayoutId() {
        return R.layout.activity_statistics_data;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public String getToolbarTitle() {
        return "数据详情";
    }

    @Override
    public void init() {

        mModel = new UserInfoModel();
        String date;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        date = df.format(calendar.getTime());
        requestOrderStatisticsInfo(date);
        requestDriverCarStatisticsInfo(date);

    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    private void requestOrderStatisticsInfo(String date) {
        Map<String, String> map = new HashMap<>();
        map.put("dates", date);
        mModel.orderStatisticsInfoModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<OrderStatisticalInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<OrderStatisticalInfo>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    List<OrderStatisticalInfo> list = commonResponse.getData();
                    showOrderStatisticsInfo(list);

                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    private void requestDriverCarStatisticsInfo(String date) {
        Map<String, String> map = new HashMap<>();
        map.put("dates", date);
        mModel.driverCarStatisticsInfoModel(this, map, false, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<DriverCarStatisticalInfo>>() {
            @Override
            public void onNext(CommonResponse<DriverCarStatisticalInfo> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    DriverCarStatisticalInfo info = commonResponse.getData();
                    showDriverAndCarStatisticsInfo(info);
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    private void showOrderStatisticsInfo(List<OrderStatisticalInfo> orderStatisticalInfoList) {
        if (orderStatisticalInfoList != null && orderStatisticalInfoList.size() > 0) {
            int size = orderStatisticalInfoList.size();
            OrderStatisticalInfo orderStatisticalInfo;
            OrderStatisticalInfo TJOrderStatisticalInfo = null;
            OrderStatisticalInfo HBOrderStatisticalInfo = null;
            for (int i = 0; i < size; i++) {
                orderStatisticalInfo = orderStatisticalInfoList.get(i);
                if (!TextUtils.isEmpty(orderStatisticalInfo.belongarea) && orderStatisticalInfo.belongarea.equals("1")) { //河北
                    HBOrderStatisticalInfo = orderStatisticalInfo;
                    todayHBOrderStatisticsInfoView.setStatisticsInfoData(getTodayOrderStatisticsInfo(orderStatisticalInfo));
                } else if (!TextUtils.isEmpty(orderStatisticalInfo.belongarea) && orderStatisticalInfo.belongarea.equals("2")) { //天津
                    TJOrderStatisticalInfo = orderStatisticalInfo;
                    todayTJOrderStatisticsInfoView.setStatisticsInfoData(getTodayOrderStatisticsInfo(orderStatisticalInfo));
                }
            }
            if (TJOrderStatisticalInfo != null && HBOrderStatisticalInfo != null) {
                allOrderStatisticsInfoView.setStatisticsInfoData(getAllOrderStatisticsInfo(TJOrderStatisticalInfo, HBOrderStatisticalInfo));
            }
        }
    }

    private List<StatisticsItemInfo> getAllOrderStatisticsInfo(OrderStatisticalInfo TJInfo, OrderStatisticalInfo HBInfo) {
        List<StatisticsItemInfo> list = new ArrayList<>();
        StatisticsItemInfo itemInfo;

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "单据";
        itemInfo.content = new BigDecimal(TJInfo.total).add(new BigDecimal(HBInfo.total)).toString();
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "抢单";
        itemInfo.content = new BigDecimal(TJInfo.robTotal).add(new BigDecimal(HBInfo.robTotal)).toString();
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "待收款";
        itemInfo.content = new BigDecimal(TJInfo.receiveTotal).add(new BigDecimal(HBInfo.receiveTotal)).toString();
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "取消";
        itemInfo.content = new BigDecimal(TJInfo.cancelTotal).add(new BigDecimal(HBInfo.cancelTotal)).toString();
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "支付";
        itemInfo.content = new BigDecimal(TJInfo.payTotal).add(new BigDecimal(HBInfo.payTotal)).toString();
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "发单未抢";
        itemInfo.content = new BigDecimal(TJInfo.sendTotal).add(new BigDecimal(HBInfo.sendTotal)).toString();
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "作废";
        itemInfo.content = new BigDecimal(TJInfo.delTotal).add(new BigDecimal(HBInfo.delTotal)).toString();
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "运输中";
        itemInfo.content = new BigDecimal(TJInfo.tranTotal).add(new BigDecimal(HBInfo.tranTotal)).toString();
        list.add(itemInfo);


        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "已付运费";
        itemInfo.content = new BigDecimal(TJInfo.amountTotal).add(new BigDecimal(HBInfo.amountTotal)).toString();
        list.add(itemInfo);
        return list;
    }

    private List<StatisticsItemInfo> getTodayOrderStatisticsInfo(OrderStatisticalInfo info) {
        List<StatisticsItemInfo> list = new ArrayList<>();
        StatisticsItemInfo itemInfo;

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "单据";
        itemInfo.content = info.totalNow;
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "抢单";
        itemInfo.content = info.robTotalNow;
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "待收款";
        itemInfo.content = info.receiveTotalNow;
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "取消";
        itemInfo.content = info.cancelTotalNow;
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "支付";
        itemInfo.content = info.payTotalNow;
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "发单未抢";
        itemInfo.content = info.sendTotalNow;
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "作废";
        itemInfo.content = info.delTotalNow;
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "运输中";
        itemInfo.content = info.tranTotalNow;
        list.add(itemInfo);


        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "已付运费";
        itemInfo.content = info.amountTotalNow;
        list.add(itemInfo);
        return list;
    }

    private void showDriverAndCarStatisticsInfo(DriverCarStatisticalInfo info) {
        todayDriverAndCarStatisticsInfoView.setStatisticsInfoData(getTodayDriverAndCarStatisticsInfo(info));
        driverAndCarStatisticsInfoView.setStatisticsInfoData(getDriverAndCarStatisticsInfo(info));

    }

    private List<StatisticsItemInfo> getTodayDriverAndCarStatisticsInfo(DriverCarStatisticalInfo info) {
        List<StatisticsItemInfo> list = new ArrayList<>();
        StatisticsItemInfo itemInfo;

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "待审核司机";
        itemInfo.content = info.drivernow;
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "已审核司机";
        itemInfo.content = info.yesdrivernow;
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "待审核车辆";
        itemInfo.content = info.drivercarnow;
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "已审核车辆";
        itemInfo.content = info.yesdrivercarnow;
        list.add(itemInfo);

        return list;
    }

    private List<StatisticsItemInfo> getDriverAndCarStatisticsInfo(DriverCarStatisticalInfo info) {
        List<StatisticsItemInfo> list = new ArrayList<>();
        StatisticsItemInfo itemInfo;

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "待审核司机";
        itemInfo.content = info.drivertotal;
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "已审核司机";
        itemInfo.content = info.yesdrivertotal;
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "待审核车辆";
        itemInfo.content = info.drivercartotal;
        list.add(itemInfo);

        itemInfo = new StatisticsItemInfo();
        itemInfo.title = "已审核车辆";
        itemInfo.content = info.yesdrivercartotal;
        list.add(itemInfo);

        return list;
    }
}
